package net.csdn.business.gateway.handler;

/**
 * @Auther: zhangyalei
 * @Date: 2022/12/5 19:43
 * @Description:
 */

import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.gateway.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.ConnectException;
import java.nio.charset.Charset;
import java.util.Map;

/**
 * 网关全局异常处理
 * @author javadaily
 */
@Slf4j
@Order(-1)
@Configuration
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GlobalErrorWebExceptionHandler implements ErrorWebExceptionHandler {

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
        ServerHttpResponse response = exchange.getResponse();
        if (response.isCommitted()) {
            return Mono.error(ex);
        }
        String error = "innernal_system_error";
        String desc="Internal Server Error";
        HttpStatus httpStatus=HttpStatus.INTERNAL_SERVER_ERROR;
        if (ex instanceof ResponseStatusException) {
            httpStatus=HttpStatus.NOT_FOUND;
            error="not_found";
            desc = "Service not found, please check the connection address";
        }else if(ex instanceof ConnectException){
            httpStatus=HttpStatus.SERVICE_UNAVAILABLE;
            error="service_unavailable";
            desc="Service Unavailable";
        }else if(ex instanceof InvalidTokenException ){
            httpStatus=HttpStatus.UNAUTHORIZED;
            error="invalid_token";
            desc = "Invalid token, please provide correct token";
        }else if(ex instanceof OAuth2AuthenticationException){
            httpStatus=HttpStatus.UNAUTHORIZED;
            error="unauthorized";
            desc = ex.getMessage();
        }else if(ex instanceof IllegalArgumentException){
            //httpStatus=HttpStatus.NOT_FOUND;
            error="rewrite_path_error";
            desc = "RewritePath regexp is error";
        }
        ex.printStackTrace();
        //log.error("ErrorWebExceptionHandler error {}",JSON.toJSON(exchange.getAttributes()));
        return ResponseUtils.webFluxResponseWriter(exchange.getResponse(), httpStatus,error,desc);
    }
}

